package com.zdyt.opc.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.da.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SampAccessTask  {

    private String taskName;

    private Integer taskId;

    private Integer period;

    private String targetUrl; // 任务采集数据后 目标发送的接口地址

    private List<String> itemIds = new ArrayList<>();

    private Map<String, String> itemId2CN;

    @JsonIgnore
    private AccessBase accessBase;


    public SampAccessTask(String taskName, Integer taskId, Integer period, AccessBase accessBase,String targetUrl) {
        this.taskName = taskName;
        this.taskId = taskId;
        this.period = period;
        this.accessBase = accessBase;
        this.targetUrl = targetUrl;
    }

    public void clear(){
        accessBase.clear();
    }

    public void unbind() throws JIException {
        accessBase.unbind();
    }

    public void bind(){
        accessBase.bind();
    }

    public boolean isBound(){
        return accessBase.isBound();
    }

    public void removeItem(String itemId){
        accessBase.removeItem(itemId);
        itemIds.remove(itemId);
    }

    public void addItems(String itemId, DataCallback dataCallback) throws JIException, AddFailedException {
        accessBase.addItem(itemId,dataCallback);
        itemIds.add(itemId);
    }

    public void addStateListener(AccessStateListener listener){
        accessBase.addStateListener(listener);

    }

    public void removeStateListener(AccessStateListener listener){
        accessBase.removeStateListener(listener);
    }
}
